home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / sml_nj / 93src.lha / src / runtime / exncode.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-09  |  6.9 KB  |  320 lines

  1. /* exncode.c
  2.  *
  3.  * COPYRIGHT (c) 1990 by AT&T Bell Laboratories.
  4.  *
  5.  * NOTE: this file should be merged with signal.c
  6.  */
  7.  
  8. #ifdef MIPS
  9. #ifdef sony_news
  10. #include <machine/cpu.h>   /* for EXC_OV */
  11. #else
  12. #ifndef SGI
  13. #include <mips/cpu.h>   /* for EXC_OV */
  14. #else
  15. #include <sys/sbd.h>   /* for EXC_OV */
  16. #endif
  17. #endif
  18. #endif
  19. #include <signal.h>
  20. #ifdef DYNIX
  21. #include <sys/types.h>
  22. #include <machine/fpu.h>
  23. #endif
  24. #include "tags.h"
  25. #include "ml_types.h"
  26.  
  27. extern int    div_e0[], overflow_e0[];
  28.  
  29. #ifdef HPUX
  30. #  define CODE_FLTDIV        0x0400
  31. #  ifdef HPPA
  32. #    define CODE_FLTOVF         12
  33. #  else
  34. #    define CODE_FLTOVF        0x1000
  35. #    define CODE_FLTUND        0x0800
  36. #    ifndef FPE_FLTOPERR_TRAP
  37. #      define FPE_FLTOPERR_TRAP   0x2000
  38. #    endif
  39. #  endif
  40. #endif
  41.  
  42. #ifdef NeXT
  43. /* these are taken from the 0.9 include files */
  44. #  define CODE_OVERFLOW        0x1c
  45. #  define CODE_ZERODIV        0x14
  46. #  define CODE_FLTDIV        0xc8
  47. #  define CODE_FLTOVF        0xd4
  48. #  define CODE_FLTUND        0xcc
  49. #endif
  50.  
  51. #ifdef THINK_C
  52. #include "MacOS.dep.h"
  53. #  define CODE_OVERFLOW        0x1c
  54. #  define CODE_ZERODIV        0x14
  55. /* these are taken from the NEXT defn above  -- e, check this! */
  56. #  define CODE_FLTDIV        0xc8
  57. #  define CODE_FLTOVF        0xd4
  58. #  define CODE_FLTUND        0xcc
  59. #endif
  60.  
  61. /* make_exn_code:
  62.  * Map a UNIX (signal, code) pair to an ML (exception, arg) pair.
  63.  */
  64. #if defined(AIX)
  65. #include <fpxcp.h>
  66.  
  67. void make_exn_code(msp,scp,sig,code)
  68. MLState_ptr         msp;
  69. struct sigcontext    *scp;
  70. int            sig, code;
  71.     {
  72.      switch (sig) 
  73.         {
  74.         case SIGTRAP:
  75.             {
  76.             fp_ctx_t    flt_context;
  77.  
  78.             fp_sh_trap_info(scp,&flt_context);
  79.                         if (flt_context.trap & FP_DIV_BY_ZERO) 
  80.                 {
  81.                                 msp->fault_exn = PTR_CtoML(div_e0+1);
  82.                                 return;
  83.                 }
  84.                         else     {
  85.                                 msp->fault_exn = PTR_CtoML(overflow_e0+1);
  86.                                 return;
  87.                                }
  88.             }
  89.                 default:
  90.                         die("Unknown signal %d\n", sig);
  91.         }
  92.     }
  93. #else
  94. void make_exn_code (MLState,scp,sig,code)
  95.     MLState_ptr MLState;
  96.     struct sigcontext *scp;
  97.     int        sig, code;
  98. {
  99.     switch (sig) {
  100. #ifdef THINK_C
  101.     case SIGTRAP:
  102. #else
  103.       case SIGFPE:
  104. #endif
  105. #if defined(HPUX) && !defined(HPPA)
  106.     if (code == 5) {
  107.         MLState->fault_exn = PTR_CtoML(div_e0+1);
  108.         return;
  109.     }
  110.     else
  111.         code &= 0x3c00;  /* grab exception status */
  112. #endif /* HPUX && !HPPA */
  113.  
  114. #ifdef HPPA
  115.     /* The cause of the floating exception could be
  116.     ** dumped into any of the floating exception registers 
  117.     */
  118.     {
  119.         struct save_state *ss;
  120.         
  121.         ss = &(scp->sc_sl.sl_ss);
  122.         if ((ss->ss_frexcp1 & 0x40000000) ||
  123.         (ss->ss_frexcp2 & 0x40000000) ||
  124.         (ss->ss_frexcp3 & 0x40000000) ||
  125.         (ss->ss_frexcp4 & 0x40000000) ||
  126.         (ss->ss_frexcp5 & 0x40000000) ||
  127.         (ss->ss_frexcp6 & 0x40000000) ||
  128.         (ss->ss_frexcp7 & 0x40000000)) 
  129.         {
  130.             MLState->fault_exn = PTR_CtoML(div_e0+1);
  131.             return;
  132.         }
  133.         else if ((ss->ss_frexcp1 & 0x20000000) ||
  134.              (ss->ss_frexcp2 & 0x20000000) ||
  135.              (ss->ss_frexcp3 & 0x20000000) ||
  136.              (ss->ss_frexcp4 & 0x20000000) ||
  137.              (ss->ss_frexcp5 & 0x20000000) ||
  138.              (ss->ss_frexcp6 & 0x20000000) ||
  139.              (ss->ss_frexcp7 & 0x20000000)) 
  140.         
  141.         {
  142.             MLState->fault_exn = PTR_CtoML(overflow_e0+1);
  143.             return;
  144.         }
  145.     }
  146. #endif /* HPPA */
  147.  
  148. #ifdef AUX
  149.     if (code == 5) { /* integer divide by zero */
  150.         MLState->fault_exn = PTR_CtoML(div_e0+1);
  151.     }
  152.     else
  153.         die("unexpected floating point error: %d\n", code);
  154.     return;
  155. #else
  156.  
  157. /* MACH maps mips exceptions to the corresponding BSD exceptions; it
  158.   does it right */
  159.  
  160. #if defined(MIPS) && !defined(MACH) 
  161.     if (code == EXC_OV) {
  162.         MLState->fault_exn = PTR_CtoML(overflow_e0+1);
  163.     }
  164.     else {
  165.       /* code contains the FP control/status register */
  166.         if (code & 0x4000) /* bit-14 is overflow */
  167.         MLState->fault_exn = PTR_CtoML(overflow_e0+1);
  168.         else if (code & 0x18000) /* bit-15 is divide by zero,
  169.                        bit-16 is 0/0 */
  170.         MLState->fault_exn = PTR_CtoML(div_e0+1);
  171.         else if (code & 0x2000 /* bit-13 is underflow */)
  172.         die("underflow should not trap\n");
  173. #ifdef SGI
  174.         else if (code == 14) /* no known symbolic constant for this */
  175.         MLState->fault_exn = PTR_CtoML(div_e0+1);
  176. #endif
  177.         else 
  178.             die("strange floating point error, %d\n",code);
  179.     }
  180.     return;
  181. #else
  182.     switch (code) {
  183. #ifdef CODE_OVERFLOW
  184.     case CODE_OVERFLOW:
  185. #endif
  186. #ifdef FPE_TRAPV_TRAP
  187.     case FPE_TRAPV_TRAP:
  188. #endif
  189. #ifdef FPE_INTOVF_TRAP
  190.     case FPE_INTOVF_TRAP:
  191. #endif
  192. #ifdef FPE_FLTOVF_TRAP
  193.     case FPE_FLTOVF_TRAP:
  194. #endif
  195. #ifdef FPE_FLTOVF_FAULT
  196.     case FPE_FLTOVF_FAULT:
  197. #endif
  198. #ifdef K_INTOVF
  199.       case K_INTOVF:
  200. #endif
  201. #ifdef CODE_FLTOVF
  202.       case CODE_FLTOVF:
  203. #endif
  204. #ifdef K_FLTOVF
  205.       case K_FLTOVF:
  206. #endif
  207. #if   defined(SPARC) && defined (MACH)
  208.     case 0x8:
  209. #endif 
  210. #if defined(HPPA)
  211.     case 14:
  212. #endif
  213.         MLState->fault_exn = PTR_CtoML(overflow_e0+1);
  214.         return;
  215. #ifdef CODE_ZERODIV
  216.       case CODE_ZERODIV:
  217. #endif
  218. #ifdef FPE_INTDIV_TRAP
  219.       case FPE_INTDIV_TRAP:
  220. #endif
  221. #ifdef FPE_FLTDIV_TRAP
  222.       case FPE_FLTDIV_TRAP:
  223. #endif
  224. #ifdef FPE_FLTDIV_FAULT
  225.       case FPE_FLTDIV_FAULT:
  226. #endif
  227. #ifdef FPE_FLTOPERR_TRAP    /* sun-3&4s generate this on 0.0/0.0 */
  228.       case FPE_FLTOPERR_TRAP:
  229. #endif
  230. #ifdef FPE_FLTINV_TRAP          /* I387's generate this on 0.0/0.0 */
  231.           case FPE_FLTINV_TRAP:
  232. #endif
  233. #ifdef CODE_FLTDIV
  234.       case CODE_FLTDIV:
  235. #endif
  236. #ifdef K_INTDIV
  237.       case K_INTDIV:
  238. #endif
  239. #ifdef K_FLTDIV
  240.       case K_FLTDIV:
  241. #endif
  242. #if    defined(SPARC) && defined(MACH)
  243.       case 0x82:
  244. #endif
  245. #if    defined(HPPA) && defined(HPUX)
  246.       case 13:   /* what's the proper symbolic name for this? XXX */
  247.         
  248. #endif
  249.         MLState->fault_exn = PTR_CtoML(div_e0+1);
  250.         return;
  251. #ifdef CODE_FLTUND
  252.       case CODE_FLTUND:
  253. #endif
  254. #ifdef K_FLTUND
  255.       case K_FLTUND:
  256. #endif
  257. #ifdef FPE_FLTUND_TRAP
  258.       case FPE_FLTUND_TRAP:
  259. #endif
  260. #ifdef FPE_FLTUND_FAULT
  261.       case FPE_FLTUND_FAULT:
  262. #endif
  263.         die("underflow should not trap\n");
  264.       default:
  265.             die("strange floating point error, %#x\n", code);
  266.     }
  267. #endif MIPS
  268. #endif AUX
  269.  
  270. #ifndef THINK_C
  271.       case SIGEMT:
  272.     die("Floating point EMT trap (68881 not installed?)\n");
  273. #endif
  274.  
  275. #ifdef HPUX
  276.       case SIGILL:
  277.     if (code == 7) {
  278.         MLState->fault_exn = PTR_CtoML(overflow_e0+1);
  279.         return;
  280.     }
  281.     else
  282.         die ("exnCode: code was %d\n",code);
  283. #endif HPUX
  284.  
  285. #ifdef AUX
  286.       case SIGILL:
  287.     if (code == 7) { /* integer overflow trap */
  288.         MLState->fault_exn = PTR_CtoML(overflow_e0+1);
  289.     }
  290.     else
  291.         die ("unexpected illegal instruction trap: %d\n",code);
  292.     return;
  293. #endif AUX
  294.  
  295. #if     defined(SPARC) && defined(MACH)
  296.       case SIGILL:
  297.     if (code == 0x87) {
  298.           MLState->fault_exn = PTR_CtoML(overflow_e0+1);
  299.           return;
  300.         } else
  301.           die ("SIGILL code 0x%x\n",code);
  302. #endif  /* defined(SPARC) && defined(MACH) */
  303. #ifdef MIPS
  304.       case SIGTRAP:
  305.     switch (code) {
  306.       case BRK_OVERFLOW:
  307.         MLState->fault_exn = PTR_CtoML(overflow_e0+1);
  308.         return;
  309.       case BRK_DIVZERO:
  310.         MLState->fault_exn = PTR_CtoML(div_e0+1);
  311.         return;
  312.       default:
  313.         die("illegal BREAK instruction");
  314.     }
  315. #endif MIPS
  316.     } /* end of switch */
  317.  
  318. } /* end of make_exn_code */
  319. #endif AIX
  320.